//
//  FeedBackViewController.m
//  ZYZB
//
//  Created by ENERGY on 2018/7/12.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "FeedBackViewController.h"
#import "IWTextView.h"
@interface FeedBackViewController ()
@property (nonatomic, strong)IWTextView * textView;
@end

@implementation FeedBackViewController(networking)
//期限栏
- (void)feedBackInform{
    [self showprogressHUD];
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{
                           @"userName":[UserModel sharedInstanced].mobile,
                           @"phone":[UserModel sharedInstanced].mobile,
                           @"token":[UserModel sharedInstanced].token,
                           @"content":self.textView.text,
                           @"t":timeStr
                           };
    //sign
    NSDictionary * signDic = @{
                               @"userName":[UserModel sharedInstanced].mobile,
                               @"phone":[UserModel sharedInstanced].mobile,
                               @"token":[UserModel sharedInstanced].token,
                               @"content":self.textView.text,
                               @"t":timeStr,
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/lend/index?api=api1002"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
//            NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
//            NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            if ([jsonData[@"code"] isEqualToString:@"000000"]) {
                [self.view makeToast:jsonData[@"msg"] duration:2 position:CSToastPositionCenter];
                
                int64_t delayInSeconds = 1.5;      // 延迟的时间
                /*
                 *@parameter 1,时间参照，从此刻开始计时
                 *@parameter 2,延时多久，此处为秒级，还有纳秒等。10ull * NSEC_PER_MSEC
                 */
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                [self.view makeToast:jsonData[@"msg"] duration:2 position:CSToastPositionCenter];
            }
//            if ([compareMD5Str isEqualToString:signResult]) {
//
//                NSLog(@"成功");
//            }else{
//                NSLog(@"失败");
//            }
        }
    }];
}
@end

@implementation FeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"用户反馈";
    
    [self setUpViews];
}

- (void)setUpViews{
    UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(18/WIDTH_6_SCALE, 22/WIDTH_6_SCALE, SCREEN_WIDTH - 36/WIDTH_6_SCALE, 63/WIDTH_6_SCALE)];
    titleLab.lineBreakMode = 0;
    titleLab.numberOfLines = 0;
    NSString * titleLabStr = @"亲爱的福乐猪用户：\n为了提供给您更好的产品体验服务，希望您能留下宝贵的意见。";
    [self label:titleLab withFont:DEF_FontSize_14 withText:titleLabStr withTextColor:getColor(@"737373") withTextAlignment:NSTextAlignmentLeft withView:self.view];
    //输入框
    self.textView = [[IWTextView alloc] initWithFrame:CGRectMake(titleLab.left, titleLab.bottom + 15/WIDTH_6_SCALE, titleLab.width, 250/WIDTH_6_SCALE)];
    self.textView.placeholder = @"请填写您的宝贵意见";
    self.textView.layer.borderColor = getColor(grayColor).CGColor;
    self.textView.layer.borderWidth = 1;
    [self.view addSubview:self.textView];
    //提交按钮
    UIButton * submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(58/WIDTH_6_SCALE,self.textView.bottom + 55/WIDTH_6_SCALE, SCREEN_WIDTH - (58*2)/WIDTH_6_SCALE, 38/WIDTH_6_SCALE)];
    [self button:submitBtn withBGColor:getColor(@"3675bd") withLayerBorderColor:nil withLayerBorderWith:0 withCornerRadius:6 withMasksToBounds:YES withViwe:self.view];
    [submitBtn setTitle:@"提交反馈" forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitBtn.titleLabel.font = DEF_FontSize_12;
    [submitBtn addTarget:self action:@selector(submitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - action
- (void)submitBtnClick:(UIButton *)sender{
    if (self.textView.text == nil || [self.textView.text  isEqual: @""] || [self.textView.text length] == 0) {
        [self.view makeToast:@"请输入反馈内容" duration:2 position:CSToastPositionCenter];
    }else{
        [self feedBackInform];//网络接口
    }
}
@end
